package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketOrderChannelMapper;
import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.service.AdminOrderService;
import com.cskaoyan.market.util.MybatisUtil;
import com.cskaoyan.market.vo.AdminOrderDetailVO;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


public class AdminOrderServiceImpl implements AdminOrderService {


    @Override
    public AdminOrderDetailVO getData(String id) {
        // 获取SqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketOrderGoodsMapper orderGoodsMapper = session.getMapper(MarketOrderGoodsMapper.class);
        // 增加orderGoods筛选条件
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria orderGoodsExampleCriteria = marketOrderGoodsExample.createCriteria();
        orderGoodsExampleCriteria.andOrderIdEqualTo(Integer.parseInt(id));
        // 获得orderGoods对象
        List<MarketOrderGoods> orderGoods = orderGoodsMapper.selectByExample(marketOrderGoodsExample);

        // 增加order筛选条件
        // 通过orderId获取order和order中的userId, 用于查找到对应的user
        MarketOrderMapper orderMapper = session.getMapper(MarketOrderMapper.class);
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria orderExampleCriteria = marketOrderExample.createCriteria();
        orderExampleCriteria.andIdEqualTo(Integer.parseInt(id));
        // 获取order对象
        List<MarketOrder> orders = orderMapper.selectByExample(marketOrderExample);
        MarketOrder order = orders.get(0);
        Integer userId = order.getUserId();


        // 增加user筛选条件
        MarketUserMapper userMapper = session.getMapper(MarketUserMapper.class);
        MarketUserExample userExample = new MarketUserExample();
        MarketUserExample.Criteria userExampleCriteria = userExample.createCriteria();
        userExampleCriteria.andIdEqualTo(userId);
        // 获取user对象
        List<MarketUser> users = userMapper.selectByExample(userExample);
        MarketUser user = users.get(0);

        // 封装AdminOrderDetailVO对象返回给controller
        AdminOrderDetailVO adminOrderDetailVO = new AdminOrderDetailVO();
        adminOrderDetailVO.setOrderGoods(orderGoods);
        adminOrderDetailVO.setUser(user);
        adminOrderDetailVO.setOrder(order);
        // 提交事务
        session.commit();
        session.close();
        return adminOrderDetailVO;
    }


    @Override
    public void updateRefundInfo(String orderId) {
        // 获取SqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketOrderMapper mapper = session.getMapper(MarketOrderMapper.class);

        // 获取对象
        MarketOrder marketOrder = new MarketOrder();
        // 状态码
        marketOrder.setOrderStatus((short) 203);
        // 更新数据
        mapper.updateByPrimaryKeySelective(marketOrder);
        // 提交事务
        session.close();
        session.close();

    }

    @Override
    public void updateShipInfo(String orderId, String shipChannel, String shipSn) {
        // 获取SqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketOrderMapper mapper = session.getMapper(MarketOrderMapper.class);

        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setOrderStatus((short) 301);
        marketOrder.setShipChannel(shipChannel);
        marketOrder.setShipSn(shipSn);
        // 更新数据
        mapper.updateByPrimaryKeySelective(marketOrder);
        // 提交事务
        session.commit();
        session.close();
    }

    @Override
    public Boolean delete(String orderId) {
        // 获取SqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketOrderMapper mapper = session.getMapper(MarketOrderMapper.class);
        // 查询要进行删除的数据
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        criteria.andIdEqualTo(Integer.parseInt(orderId));
        // 获取order对象
        MarketOrder marketOrder = mapper.selectByExample(marketOrderExample).get(0);
        // 获取orderStatus, 分情况进行反馈
        Short orderStatus = marketOrder.getOrderStatus();
        if (orderStatus == 102 || orderStatus == 103 || orderStatus == 203 || orderStatus == 401 || orderStatus == 402) {
            // 对数据进行删除
            mapper.deleteByPrimaryKey(Integer.parseInt(orderId));
            //  提交事务
            session.commit();
            session.close();
            return true;
        }
        session.commit();
        session.close();
        // 其余情况下的订单不可删除
        return false;
    }


    @Override
    public List<MarketOrder> list(String page, String limit, String sort, String order, String userId, String orderSn, String start, String end, List<Short> status) {

        // 获得mapper
        SqlSession session = MybatisUtil.getSession();
        MarketOrderMapper mapper = session.getMapper(MarketOrderMapper.class);

        // 创建查询条件
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();

        // 做逻辑删除判断
        criteria.andDeletedEqualTo(false);

        // 做用户id判断
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));

        }
        // 做订单编号判断
        if (!StringUtils.isEmpty(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);

        }
        // 做时间判断
        if (!StringUtils.isEmpty(start) && !StringUtils.isEmpty(end)) {
            // 做时间类型的转换
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime startTime = LocalDateTime.parse(start, formatter);
            LocalDateTime endTime = LocalDateTime.parse(end, formatter);
            // 做判断
            criteria.andUpdateTimeBetween(startTime, endTime);

        }
        // 做订单状态查询判断
        if (status != null && status.size() != 0) {
            criteria.andOrderStatusIn(status);
        }

        // 做排序
        marketOrderExample.setOrderByClause(sort + " " + order);

        // 做分页设置
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketOrder> marketOrders = mapper.selectByExample(marketOrderExample);

        session.commit();
        session.close();

        return marketOrders;
    }

    @Override
    public List<MarketOrderChannel> channel() {
        SqlSession sqlSession = MybatisUtil.getSession();
        MarketOrderChannelMapper channelMapper = sqlSession.getMapper(MarketOrderChannelMapper.class);
        List<MarketOrderChannel> channels = channelMapper.selectAll();
        return channels;
    }
}


